<?php

require_once 'Connection.php';

class Functions {

    private $con;

    function __construct() {
        $conn = new Connection();
        $this->con = $conn->configDB();
    }

    function generateString($string, $length) {
        $result = "";
        for ($i = 0; $i < $length; $i++) {
            $rand = rand(0, strlen($string) - 1);
            $result .= $string[$rand];
        }
        return $result;
    }

    function getCharFromR($string, $n) {
        return substr($string, strlen($string) - $n, $n);
    }

    function getCurrentDate() {
        date_default_timezone_set("Asia/Ho_Chi_Minh");
        $dayInWeekE = date('D');
        $day = date('d');
        $month = date('m');
        $year = date('Y');
        $hour = date('H');
        $minute = date('i');
        $second = date('s');
        $dayInWeek = $this->getDayInWeekNumber($dayInWeekE);
        $arrDate = array();
        $arrDate['Date'] = $day . "-" . $month . '-' . $year;
        $arrDate['Year'] = $year;
        $arrDate['Month'] = $month;
        $arrDate['Day'] = $day;
        $arrDate['DayInWeek'] = $dayInWeek;
        $arrDate['Hour'] = $hour;
        $arrDate['Minute'] = $minute;
        $arrDate['Second'] = $second;
        return $arrDate;
    }

    function getDayInWeekNumber($dayInWeekE) {
        $dayInWeek = 0;
        switch ($dayInWeekE) {
            case "Mon" :
                $dayInWeek = 2;
                break;
            case "Tue" :
                $dayInWeek = 3;
                break;
            case "Wed" :
                $dayInWeek = 4;
                break;
            case "Thu" :
                $dayInWeek = 5;
                break;
            case "Fri" :
                $dayInWeek = 6;
                break;
            case "Sat" :
                $dayInWeek = 7;
                break;
            case "Sun" :
                $dayInWeek = 8;
                break;
            default :
                break;
        }
        return $dayInWeek;
    }

    function numToString($num, $n) {
        $result = "";
        if ($n == 4) {
            if ($num < 10) {
                $result = "000" . $num;
            } else if ($num < 100) {
                $result = "00" . $num;
            } else if ($num < 1000) {
                $result = "0" . $num;
            } else {
                $result = $num;
            }
        } else if ($n == 3) {
            if ($num < 10) {
                $result = "00" . $num;
            } else if ($num < 100) {
                $result = "0" . $num;
            } else {
                $result = $num;
            }
        } else if ($n == 2) {
            if ($num < 10) {
                $result = "0" . $num;
            } else {
                $result = $num;
            }
        }
        return $result;
    }

    function getSessionLogin() {
        session_start();
        return isset($_SESSION['userLog']) ? $_SESSION['userLog'] : NULL;
    }

    function getSessionLoginAgent() {
        session_start();
        return isset($_SESSION['agentLog']) ? $_SESSION['agentLog'] : NULL;
    }

    function checkHaveChild($playerid) {
        $strSelect = "SELECT COUNT(PlayerID) as mems from players WHERE ParentID ='$playerid'";
        $strQuery = mysqli_query($this->con, $strSelect);
        $row = mysqli_fetch_array($strQuery);
        return $row;
    }

}
